﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HomeSuiteHome.Properties;
using TPLinkNetworking;

namespace HomeSuiteHome
{
    public partial class Module : UserControl
    {
        public TPLinkConnection.SmartLink moduleLink;
        public Module(TPLinkConnection.SmartLink moduleSmartLink)
        {
            InitializeComponent();
            txt_ModuleName.Width = toolRw_Module.Width - 300;
            moduleLink = moduleSmartLink;
            UpdateModuleInformation();

        }

        private void toolRw_Module_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolRw_Module_Resize(object sender, EventArgs e)
        {
            // reside the module name to fill the toolbox
            txt_ModuleName.Width = toolRw_Module.Width - 300;
        }

        private void btn_Module_OnOff_Click(object sender, EventArgs e)
        {

            var connection = new TPLinkConnection();
            moduleLink.ModuleInformation = connection.SetRelayState(moduleLink, btn_Module_OnOff.Checked);
            UpdateModuleInformation();
        }

        public void UpdateModuleInformation()
        {
            var connection = new TPLinkConnection();

            moduleLink.ModuleInformation = connection.GetModuleInformation(moduleLink);

            txt_ModuleName.Text = moduleLink.ModuleInformation.system.get_sysinfo.alias;

            //Remove items that can't be filled.
            if (moduleLink.ModuleInformation.system.get_sysinfo.dev_name == "Wi-Fi Smart Plug")
            {
                lbl_watts.Text = @" - - ";
            }
            else
            {
                // go get the usage information for the first tim
                moduleLink.ModuleInformation = connection.GetRealTimeEnergyUsage(moduleLink);
                if (moduleLink.ModuleInformation.EnergyStats.Any())
                {
                    var firstOrDefault = moduleLink.ModuleInformation.EnergyStats.OrderByDescending(a => a.emeter.timeStamp)
                        .FirstOrDefault();
                    if (firstOrDefault != null)
                    {
                        lbl_watts.Text = Convert.ToString((int)
                            firstOrDefault
                                .emeter.get_realtime.power);
                    }
                }
            }
            //btn_Module_OnOff.SetPropertyThreadSafe(.Invoke((MethodInvoker)delegate { panel_Modules.Controls.Add(newModule); });

            btn_Module_OnOff.BackgroundImage = moduleLink.ModuleInformation.system.get_sysinfo.relay_state == 1
                ? Resources.led_and_label_on
                : Resources.led_and_label_off;
            btn_Module_OnOff.Checked = moduleLink.ModuleInformation.system.get_sysinfo.relay_state == 1;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateModuleInformation();
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            new ModuleSettings(moduleLink).ShowDialog();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            new reporting(new List<Module>() {this}).Show();
        }
    }
}
